﻿using System.Collections.Generic;
using ArtificialIntelligence.CombatComponent;
using FarseerGames.FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using XNA_2DFramwork;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace XnaGame
{
    class Game2:Game
    {
        private  GraphicsDeviceManager _graphics;
        
        private  PhysicsSimulator _ps = new PhysicsSimulator(new Vector2(0, 10));
        SpriteBatch spriteBatch;
        public Entity E;
        public Rectangel B;
        public Circle C;
        public Agent A1;
        public Agent A2;


        private InferenceMachine inferenceMacine;

        public Game2()
        {
            VisualObject._lVisualObject = new List<VisualObject>();
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            inferenceMacine = new InferenceMachine(this);
        }

        protected override void Initialize()
        {
            _graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
            _graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
            _graphics.ApplyChanges();

            A1 = new Agent(this,200,200,new Vector2(700, 200), "6609_217353725572_735865572_7884300_5285461_n",
                          _graphics.GraphicsDevice, _ps)
                    {
                        Geom =
                        {
                            CollisionCategories = CollisionCategory.Cat1,
                            CollidesWith = CollisionCategory.Cat2
                        }
                    };

            A2 = new Agent(this, 50, 50, new Vector2(400, 200), "ALIEN_01_01", _graphics.GraphicsDevice, _ps)
                     {
                         Geom =
                             {
                                 CollisionCategories = CollisionCategory.Cat1,
                                 CollidesWith = CollisionCategory.Cat2
                             }
                     };



            E = new Entity(this, new Vector2(640, 750), 1280, 100, "6609_217353725572_735865572_7884300_5285461_n",
                           _graphics.GraphicsDevice, _ps)
                    {
                        Body = { IsStatic = true },
                        Geom =
                        {
                            CollisionCategories = CollisionCategory.Cat1,
                            CollidesWith = CollisionCategory.All
                        }
                    };

            B = new Rectangel(this, 100, 100, new Vector2(400, 600), Color.Black, _graphics.GraphicsDevice, _ps)
                    {
                        Geom = { CollisionCategories = CollisionCategory.Cat2
                            ,
                                 CollidesWith = CollisionCategory.Cat1
                        },
                        Body = { IsStatic = true }
                    };

            C = new Circle(this, 50, new Vector2(800, 400), Color.Black, _graphics.GraphicsDevice, _ps)
                    {
                        Body = { IsStatic = true },
                        Geom =
                            {
                                CollisionCategories = CollisionCategory.Cat3,
                                CollidesWith = CollisionCategory.Cat1
                            }
                    };


            inferenceMacine.CreateSchedule(Schedule.ScheduleType.GetObject);
            inferenceMacine.CreateSchedule(Schedule.ScheduleType.GetWeapon);

            VisualObject._lVisualObject.Add(A1);
           // VisualObject._lVisualObject.Add(E);
            VisualObject._lVisualObject.Add(B);
            VisualObject._lVisualObject.Add(C);
            VisualObject._lVisualObject.Add(A2);

            VisualObject._lVisualObject.Add(inferenceMacine);
            
            inferenceMacine._LAgents.Add(A1);
            inferenceMacine._LAgents.Add(A2);
            
            base.Initialize();
        }

        private ContentManager _contentManager;
        private SoundEffect _soundEffect;
        private string _soundName;

        protected override void LoadContent()
        {

            // Create a new SpriteBatch, which can be used to draw textures.
             _soundName = "siren03";
            _contentManager = new ContentManager(Services, @"Content\");
           
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            
            // TODO : Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();
            
            // TODO: Add your update logic here
            foreach (VisualObject obj in VisualObject._lVisualObject)
                obj.Update();
            _ps.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            foreach (VisualObject obj in VisualObject._lVisualObject)
                obj.Draw();
            _soundEffect = _contentManager.Load<SoundEffect>(_soundName);
            _soundEffect.Dispose();
            base.Draw(gameTime);
        }
        
    }
}